package com.ilop.sthome.mvp.present.device;

import android.animation.ValueAnimator;
import android.content.Context;
import android.util.Log;
import android.widget.LinearLayout;

import com.example.common.mvp.BasePresenterImpl;
import com.example.common.utils.DisplayUtils;
import com.ilop.sthome.data.enums.SmartDevice;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.mvp.contract.device.DeviceForTypeContract;
import com.ilop.sthome.utils.greenDao.utils.DeviceDaoUtil;
import com.siterwell.familywellplus.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author skygge
 * @date 2020/7/13.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：
 */
public class DeviceForTypePresenter extends BasePresenterImpl<DeviceForTypeContract.IView> implements DeviceForTypeContract.IPresent {
    private static final String TAG = DeviceForTypePresenter.class.getSimpleName();
    private Context mContext;
    private boolean isOpen = false;
    private List<DeviceInfoBean> mDeviceList;
    private List<String> mCurrentDeviceType;

    public DeviceForTypePresenter(Context mContext) {
        this.mContext = mContext;
        mDeviceList = new ArrayList<>();
    }

    @Override
    public void findDeviceByType(List<String> deviceType) {
        mDeviceList.clear();
        mCurrentDeviceType = deviceType;
        if (deviceType != null) {
            for (String typeCode : deviceType) {
                Log.i(TAG, "findDeviceByType: " + typeCode);
                if ("GATEWAY".equals(typeCode)){
                    List<DeviceInfoBean> mList = DeviceDaoUtil.getInstance().findAllGateway();
                    mDeviceList.addAll(mList);
                }else if ("IPC".equals(typeCode)){
                    List<DeviceInfoBean> mList = DeviceDaoUtil.getInstance().findAllCamera();
                    mDeviceList.addAll(mList);
                }else {
                    List<DeviceInfoBean> mList = DeviceDaoUtil.getInstance().findDeviceByType(typeCode);
                    mDeviceList.addAll(mList);
                }
            }
        }else {
            mDeviceList = DeviceDaoUtil.getInstance().findAllDeviceOrderByGateway();
        }
        if (mDeviceList.size()>0){
            mView.showDeviceList(mDeviceList);
        }else {
            mView.showDeviceIsEmpty();
        }
    }

    @Override
    public void findAllDeviceType() {
        List<DeviceInfoBean> deviceList = DeviceDaoUtil.getInstance().findAllDevice();
        List<String> deviceType = new ArrayList<>();
        deviceType.add(mContext.getString(R.string.all_device));
        if (deviceList.size()>0){
            for (DeviceInfoBean device:deviceList) {
                if (device.getDevice_ID() == -1){
                    String name = mContext.getString(R.string.ali_camera);
                    if (!deviceType.contains(name)){
                        deviceType.add(name);
                    }
                }else if (device.getDevice_ID() == 0){
                    String name = mContext.getString(R.string.ali_gateway);
                    if (!deviceType.contains(name)){
                        deviceType.add(name);
                    }
                }else {
                    String name = mContext.getString(SmartDevice.getType(device.getDevice_type()).getTypeStrId());
                    if (!deviceType.contains(name)){
                        deviceType.add(name);
                    }
                }
            }
        }
        mView.showDeviceType(deviceType);
    }

    @Override
    public void findAllSubDevice() {
        List<DeviceInfoBean> mGateway = DeviceDaoUtil.getInstance().findAllGateway();
        List<DeviceInfoBean> mList = DeviceDaoUtil.getInstance().findAllSubDevice();
        if (mGateway.size() > 0 && mList.size() <= 0){
            mView.showLoadingView();
        }
    }

    @Override
    public void refreshByPosition(String deviceType) {
        if (mDeviceList.size()>0){
            if (mCurrentDeviceType !=null && mCurrentDeviceType.contains(deviceType)){
                findDeviceByType(mCurrentDeviceType);
            }else {
                findDeviceByType(null);
            }
        }
    }

    @Override
    public void showAllDeviceType(LinearLayout layout, int viewHeight) {
        int original = DisplayUtils.dip2px(mContext, 48);
        ValueAnimator mAnimator;
        if (isOpen){
            mAnimator = ValueAnimator.ofInt(viewHeight + original, original);
            isOpen = false;
        }else {
            mAnimator = ValueAnimator.ofInt(original, viewHeight + original);
            isOpen = true;
        }
        mView.showArrowIcon(isOpen);
        mAnimator.addUpdateListener(animation -> {
            layout.getLayoutParams().height = (int) animation.getAnimatedValue();
            layout.requestLayout();
        });
        mAnimator.setDuration(300);
        mAnimator.start();
    }
}
